﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using Tesla.Math;

namespace Tesla.Graphics {
    /// <summary>
    /// Render bucket for managing renderables to be drawn with an orthographic projection.
    /// </summary>
    public sealed class OrthoRenderBucket : RenderBucket {
        private ICamera _orthoCamera;
        private ICamera _userOverride;

        /// <summary>
        /// Gets or sets an override orthographic camera to use. If this is null,
        /// a default camera is used that uses the Renderer.CurrentCamera's viewport.
        /// </summary>
        public ICamera OrthoCamera {
            get {
                if(_userOverride == null) {
                    return _orthoCamera;
                } else {
                    return _userOverride;
                }
            }
            set {
                _userOverride = value;
            }
        }

        /// <summary>
        /// Creates a new OrthoRenderBucket.
        /// </summary>
        /// <param name="renderer">Renderer to use</param>
        /// <param name="comparer">Bucket comparer to use</param>
        public OrthoRenderBucket(IRenderer renderer, IRenderBucketComparer comparer)
            : base(renderer, comparer) {
                _orthoCamera = new Camera(new Viewport());
                _orthoCamera.SetFrame(new Vector3(-.5f, -.5f, 0), Matrix.Identity);
        }

        /// <summary>
        /// Renders all renderables in the bucket.
        /// </summary>
        public override void Render() {
            if(Count > 0) {
                if(_userOverride == null) {
                    Viewport viewport = Renderer.CurrentCamera.Viewport;
                    _orthoCamera.Viewport = viewport;
                    _orthoCamera.SetProjection(ProjectionMode.Orthographic, 0, viewport.Width, viewport.Height, 0, 0, 1);
                    _orthoCamera.Update();
                    ICamera oldcam = Renderer.CurrentCamera;
                    Renderer.CurrentCamera = _orthoCamera;
                    base.Render();
                    Renderer.CurrentCamera = oldcam;
                } else {
                    ICamera oldcam = Renderer.CurrentCamera;
                    Renderer.CurrentCamera = _userOverride;
                    base.Render();
                    Renderer.CurrentCamera = oldcam;
                }
            }
        }
    }
}
